import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'London', 'Paris', 'Tokyo']}
df = pd.DataFrame(data, index=['id1', 'id2', 'id3', 'id4'])


print("loc 选择单行:")
print(df.loc['id2'])

print("\nloc 选择多行（标签列表）:")
print(df.loc[['id1', 'id3']])

print("\nloc 选择多行（标签切片）:")
print(df.loc['id2':'id4'])

print("\nloc 选择单列:")
print(df.loc[:, 'Age'])

print("\nloc 选择多列:")
print(df.loc[:, ['Name', 'City']])

print("\nloc 选择特定行和列:")
print(df.loc['id1', 'Name'])
print(df.loc[['id2', 'id4'], ['Age', 'City']])

print("\nloc 布尔索引:")
print(df.loc[df['Age'] > 30])

print("\niloc 选择单行:")
print(df.iloc[1])

print("\niloc 选择多行（位置列表）:")
print(df.iloc[[0, 2]])

print("\niloc 选择多行（位置切片）:")
print(df.iloc[1:4])

print("\niloc 选择单列:")
print(df.iloc[:, 1])

print("\niloc 选择多列:")
print(df.iloc[:, [0, 2]])

print("\niloc 选择特定行和列:")
print(df.iloc[0, 0])
print(df.iloc[[1, 3], [1, 2]])

print("\niloc 布尔索引:")
print(df.iloc[(df['Age'] > 30).values])